const db = wx.cloud.database()
const app = getApp()//你时间不多 哪里你要跟老师讲 我给你说哪里

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		totalBalance: 0,
		activeNames: [0],
		activeNamesMonth: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
		activeNamesDay: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']

	},

	// VantUI组件的事件
	onItemChange: function (e) {
		console.log('e', e)
		this.setData({
			activeNames: e.detail
		})
	},

	onItemChangeYear: function (e) {
		this.setData({
			activeNamesYear: e.detail
		})
		console.log("年", e.detail)
	},

	onItemChangeMonth: function (e) {
		console.log("月", e)
		this.setData({
			activeNamesMonth: e.detail
		})
	},

	onItemChangeDay: function (e) {
		this.setData({
			activeNamesDay: e.detail
		})
		console.log("日", e.detail)
	},

	// 获取在所有account账户中的所有记录
	async getAllAccountData() {
		const MAX_LIMIT = 20
		// 先取出集合记录总数 用where匹配当前的用户的openid 调用该用户的全部数据
		let countResult = await db.collection("allAccount").where({
			_openid: app.globalData.openid
		}).count()

		let total = countResult.total
		// 计算需分几次取
		let batchTimes = Math.ceil(total / 20)
		let tasks = []
		for (let i = 0; i < batchTimes; i++) {
			await db.collection("allAccount").skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
				.then(res => {
					tasks = tasks.concat(res.data)
					console.log('第', i, '次', res.data)
				})
		}

		let oldDataArray = tasks
		let finalAccountData = [] // 树形数据扁平化
		for (let i = 0; i < oldDataArray.length; i++) {
			let tempData = {
				recordAccount: oldDataArray[i].recordAccount,
				recordAmount: oldDataArray[i].recordAmount
			}
			if (oldDataArray[i].recordAccount == "现金") {
				finalAccountData[0] = tempData
			} else if (oldDataArray[i].recordAccount == "花呗") {
				finalAccountData[1] = tempData
			} else if (oldDataArray[i].recordAccount == "京东白条") {
				finalAccountData[2] = tempData
			} else if (oldDataArray[i].recordAccount == "信用卡") {
				finalAccountData[3] = tempData
			} else if (oldDataArray[i].recordAccount == "银行卡") {
				finalAccountData[4] = tempData
			} else if (oldDataArray[i].recordAccount == "微信钱包") {
				finalAccountData[5] = tempData
			} else if (oldDataArray[i].recordAccount == "支付宝") {
				finalAccountData[6] = tempData
			} else if (oldDataArray[i].recordAccount == "余额宝") {
				finalAccountData[7] = tempData
			} else if (oldDataArray[i].recordAccount == "校园卡") {
				finalAccountData[8] = tempData
			} else if (oldDataArray[i].recordAccount == "QQ钱包") {
				finalAccountData[9] = tempData
			} else if (oldDataArray[i].recordAccount == "公交卡") {
				finalAccountData[10] = tempData
			}

		}
		console.log("前finalAccountData", finalAccountData)

		let array = [
			{ "recordAccount": "现金", "recordAmount": "0" },
			{ "recordAccount": "花呗", "recordAmount": "0" },
			{ "recordAccount": "京东白条", "recordAmount": "0" },
			{ "recordAccount": "信用卡", "recordAmount": "0" },
			{ "recordAccount": "银行卡", "recordAmount": "0" },
			{ "recordAccount": "微信钱包", "recordAmount": "0" },
			{ "recordAccount": "支付宝", "recordAmount": "0" },
			{ "recordAccount": "余额宝", "recordAmount": "0" },
			{ "recordAccount": "校园卡", "recordAmount": "0" },
			{ "recordAccount": "QQ钱包", "recordAmount": "0" },
			{ "recordAccount": "公交卡", "recordAmount": "0" },

		]
		 // 数据验证 判断取回来的数据是否为空，如果为空那就array数据的数据赋值给 finalAccountData
		for (let i = 0; i < 11; i++) {
			if (finalAccountData[i] == null) {
				finalAccountData[i] = array[i]
			}
		}


		let totalBalance = 0
		// for循环 获取所有账户的总额 赋值给totalBalance变量 再把totalBalance变量赋值给全局的app.globalData.totalBalance变量
		for (let i = 0; i < finalAccountData.length; i++) { // 类型转换
			console.log(i, " : ", parseFloat(finalAccountData[i].recordAmount))
			totalBalance = totalBalance + parseFloat(finalAccountData[i].recordAmount)
		}
		app.globalData.totalBalance = totalBalance
		app.globalData.cashAccountBalance = finalAccountData[0].recordAmount
		app.globalData.huabeiAccountBalance = finalAccountData[1].recordAmount
		app.globalData.jdAccountBalance = finalAccountData[2].recordAmount
		app.globalData.creditCardBalance = finalAccountData[3].recordAmount
		app.globalData.bankCardBalance = finalAccountData[4].recordAmount
		app.globalData.wechatBalance = finalAccountData[5].recordAmount
		app.globalData.zhifubaoBalance = finalAccountData[6].recordAmount
		app.globalData.yuebaoBalance = finalAccountData[7].recordAmount
		app.globalData.schoolCardBalance = finalAccountData[8].recordAmount
		app.globalData.qqBalance = finalAccountData[9].recordAmount
		app.globalData.busCardBalance = finalAccountData[10].recordAmount
		this.setData({
			allAccountData: tasks,
			totalBalance
		})

	},

	/**
	 * 获取otherRecord集合中所有的记录
	 * 小程序端在获取集合数据时服务器一次默认并且最多返回 20 条记录，
	 */
	async getOtherRecord() {
		try {
			await this.getAllAccountData()

			const MAX_LIMIT = 20
			// 先取出集合记录总数
			let countResult = await db.collection("otherRecord").where({
				_openid: app.globalData.openid
			}).count()
			let total = countResult.total
			console.log("total", total)

			// 计算需分几次取
			let batchTimes = Math.ceil(total / 20)
			console.log("batchTimes", batchTimes)
			let tasks = []
			for (let i = 0; i < batchTimes; i++) {
				await db.collection("otherRecord").skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
					.then(res => {
						tasks = tasks.concat(res.data)
						console.log('第', i, '次', res.data)
					})

			}
			console.log("数据", tasks)
			let tempData = tasks.concat(this.data.allAccountData)

			this.setData({
				allData: tempData
			})
			console.log("最终的allData：", this.data.allData)
			await this.getEachTime()

		} catch (err) {

		}
	},


	// 定义数组的排序规则
	sortMonthRule: function (a, b) {
		return a.recordMonth - b.recordMonth
	},

	// 定义数组的排序规则
	sortDayRule: function (a, b) {
		return a.recordDay - b.recordDay
	},

	// 遍历allData数组，取出年 月 日
	getEachTime: function () {
		let data = this.data.allData
		let yearArray = []
		let monthArray = []
		let dayArray = []
		for (let i = 0; i < data.length; i++) {
			if (yearArray.indexOf(data[i].recordYear) == -1) {
				yearArray.push(data[i].recordYear)
			}

			let tempMonthData = {
				recordYear: data[i].recordYear,
				recordMonth: data[i].recordMonth
			}

			// 先将对象数组和该对象元素转化为字符串，使用JSON.stringify()，再来判断该对象是否在该数组中
			if (JSON.stringify(monthArray).indexOf(JSON.stringify(tempMonthData)) == -1) {
				monthArray.push(tempMonthData)
			}

			let tempDayData = {
				recordYear: data[i].recordYear,
				recordMonth: data[i].recordMonth,
				recordDay: data[i].recordDay
			}

			if (JSON.stringify(dayArray).indexOf(JSON.stringify(tempDayData)) == -1) {
				dayArray.push(tempDayData)
			}
		}
		yearArray.sort()
		monthArray.sort(this.sortMonthRule)
		dayArray.sort(this.sortDayRule)
		this.setData({
			yearArray: yearArray,
			monthArray: monthArray,
			dayArray: dayArray,
			activeNamesYear: yearArray
		})
		console.log("yearArray", yearArray)
		console.log("monthArray", monthArray)
		console.log("dayArray", dayArray)
		wx.hideLoading()

		// if (this.data.totalBalance == 0) {
		// 	wx.showModal({
		// 		title: '提示',
		// 		content: '检测到您的账户总额为0元，请查看[账户]或记一笔余额 ',
		// 		showCancel: false,
		// 		success(res) {
		// 			if (res.confirm) {
		// 				wx.switchTab({
		// 					url: '../account/account',
		// 				})
		// 			}
		// 		}
		// 	})
		// }


	},

	// 点击放大查看记录的图片
	viewImg: function (e) {

		wx.previewImage({
			urls: e.currentTarget.dataset.recordimgpath
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		/**
		 * 仅在页面第一次加载时显示loading，之后再进入页面不显示loading，
		 * 在onShow()函数中调用getOtherRecord()方法，重新获取数据，提升小程序的使用体验
		 */
		wx.showLoading({
			title: '拼命加载中...'
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getOtherRecord()
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	onShareAppMessage: function () {
		return {
			title: "账单日记",
			desc: "方便快捷的记账小程序",
			path: "/pages/welcome/welcome"
		}
	}

})